package com.jiuair.cloud.data.revenue.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.jiuair.cloud.data.revenue.entity.UsrRole;
import com.jiuair.cloud.data.revenue.result.PageParams;
import com.jiuair.cloud.data.revenue.result.Result;
import com.jiuair.cloud.data.revenue.service.UsrRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.*;

/**
 *  前端控制器
 *
 * @author deng
 * @date 2020-08-27
 */
@Api(value = "", tags = "")
    @RestController
@RequestMapping("usrRole")
    public class UsrRoleController {

    @Autowired
    private UsrRoleService targetService;

    /**
    * 获取分页数据
    *
    * @return
    */
    @ApiOperation(value = "获取分页数据-", notes = "获取分页数据")
    @GetMapping(value = "/list")
    public Result<IPage<UsrRole>> list(@RequestParam(required = false) Map map){
        PageParams pageParams = new PageParams(map);
        UsrRole query = pageParams.mapToObject(UsrRole.class);
        QueryWrapper<UsrRole> queryWrapper = new QueryWrapper();
        return Result.ok().data(targetService.page(pageParams,queryWrapper));
    }

    /**
     * 根据ID查找数据
     */
    @ApiOperation(value = "根据ID查找数据-", notes = "根据ID查找数据")
    @ResponseBody
    @RequestMapping("/get")
    public Result<UsrRole> get(@RequestParam("id") Long id){
        UsrRole entity = targetService.getById(id);
        return Result.ok().data(entity);
    }

    /**
    * 添加数据
    * @return
    */
    @ApiOperation(value = "添加数据-", notes = "添加数据")
    @ApiImplicitParams({
         @ApiImplicitParam(name = "code", required = true, value = "", paramType = "form"),
         @ApiImplicitParam(name = "name", required = true, value = "", paramType = "form"),
         @ApiImplicitParam(name = "description", required = true, value = "", paramType = "form"),
        @ApiImplicitParam(name = "usertype", required = true, value = "", paramType = "form")
            })
    @PostMapping("/add")
    public Result add(
        @RequestParam(value = "code") String code,
        @RequestParam(value = "name") String name,
        @RequestParam(value = "description") String description,
        @RequestParam(value = "usertype") String usertype
            ){
        UsrRole entity = new UsrRole();
        entity.setCode(code);
        entity.setName(name);
        entity.setDescription(description);
        entity.setUsertype(usertype);
        targetService.save(entity);
        return Result.ok();
    }

    /**
    * 新增数据
    * @return
    */
    @ApiOperation(value = "保存数据-", notes = "保存数据")
    @PostMapping("/save")
    @ResponseBody
    public Result add(@RequestBody UsrRole usrRole){
        Result Result=new Result();
        targetService.save(usrRole);
        return Result.ok();
        }


    /**
    * 更新数据
    * @return
    */
    @ApiOperation(value = "更新数据-", notes = "更新数据")
    @ApiImplicitParams({
                    @ApiImplicitParam(name = "id", required = true, value = "用户角色id", paramType = "form"),
                    @ApiImplicitParam(name = "code", required = true, value = "", paramType = "form"),
                    @ApiImplicitParam(name = "name", required = true, value = "", paramType = "form"),
                    @ApiImplicitParam(name = "description", required = true, value = "", paramType = "form"),
                    @ApiImplicitParam(name = "usertype", required = true, value = "", paramType = "form")
        })
        @PostMapping("/update")
        public Result update(
                @RequestParam(value = "id") Long id,
                @RequestParam(value = "code") String code,
                @RequestParam(value = "name") String name,
                @RequestParam(value = "description") String description,
                @RequestParam(value = "usertype") String usertype
        ){
            UsrRole entity = new UsrRole();
                    entity.setId(id);
                    entity.setCode(code);
                    entity.setName(name);
                    entity.setDescription(description);
                    entity.setUsertype(usertype);
                targetService.updateById(entity);
                return Result.ok();
        }

    /**
    * 删除数据
    * @return
    */
    @ApiOperation(value = "删除数据-", notes = "删除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", required = true, value = "id", paramType = "form")
    })
    @PostMapping("/remove")
    public Result remove(
            @RequestParam(value = "id") Long id
    ){
            targetService.removeById(id);
            return Result.ok();
      }

    /**
    * 批量删除数据
    * @return
    */
    @ApiOperation(value = "批量删除数据-", notes = "批量删除数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", required = true, value = "多个用,号隔开", paramType = "form")
    })
    @PostMapping("/batch/remove")
    public Result batchRemove(
                @RequestParam(value = "ids") String ids
            ){
            targetService.removeByIds(Arrays.asList(ids.split(",")));
            return Result.ok();
     }

}
